﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using DomainModel;
using BIZ;
using System.Collections;

namespace joyAdmin.Controllers
{
    [DomainConfirmationFilter]
    [BIZ.AdminFilter]
    public class UserCardController : Controller
    {
        //
        // GET: /UserCard/

        UserCardOper userCardOper = new UserCardOper();
        UserCardCostOper cardCostOper = new UserCardCostOper();
        UserOper userOper = new UserOper();
        IncomeExpensesOper incomeExpensesOper = new IncomeExpensesOper();
        #region 会员充值卡
        public ActionResult UserCardManage()
        {
            cmsOper.CheckUserRights("UserCardManage");
            return View();
        }


        public ActionResult checkCard()
        {
            string card = Request.Form["card"];
            int id = Request.Form["id"] == null ? 0 : int.Parse(Request.Form["id"]);
            bool flag = userCardOper.getCountByCard(card,id);
            if (flag)
                return Json(1);
            else
                return Json(0);
        }

        [ValidateInput(false)]
        public ActionResult saveUserCard()
        {
            UserCard _staff = new JavaScriptSerializer().Deserialize<UserCard>(Request.Form["userCard"].ToString());
            string str = "-1";
            string[] arr = Request.Form["card"].ToString().Split('，');
            for (int i = 0; i < arr.Length; i++)
            {                
                bool f = userCardOper.getCountByCard(arr[i].ToString(), _staff.uc_id);
                if (f)
                {
                    if (str != "-1")
                        str = str + arr[i].ToString() + ";";
                    else
                        str = arr[i].ToString() + ";";
                    continue;
                }
                if (_staff.uc_id != 0)
                {
                    _staff.uc_lastUser = ((DomainModel.cmsuser)Session["cmsuser"]).id;
                    _staff.uc_updateTime = DateTime.Now;
                    _staff.uc_addTime = _staff.uc_addTime.ToLocalTime();
                    userCardOper.save(_staff);
                }
                else
                {
                    _staff.uc_card = arr[i].ToString();
                    _staff.uc_addTime = DateTime.Now;
                    _staff.uc_user = ((DomainModel.cmsuser)Session["cmsuser"]).id;
                    userCardOper.save(_staff);
                    _staff.uc_id = 0;
                }
            }
            return Json(str);
        }

        public JsonResult getUserCardbyid()
        {
            string id = Request.Form["res"];
            IList list = userCardOper.getUserCardbyid(int.Parse(id));
            if (list.Count > 0)
                return Json(list[0]);
            else
                return Json("");
        }

        public JsonResult deleteUserCard()
        {
            int id =int.Parse(Request.Form["res"]);
            string card = Request.Form["card"];
            bool f = cardCostOper.checkHaveCostByCardId(id);//判断有没有记录，有的话不允许删除
            if (f)
                return Json("n");
            else
            {
                userCardOper.WriteOperationLog(((DomainModel.cmsuser)Session["cmsuser"]).id, "UserCard", id, "删除 卡号：" + card);
                return Json(userCardOper.delete(id));
            }
        }

        //

        public JsonResult logoutUserCard()
        {
            string card = Request.Form["card"];
            string fy = Request.Form["fy"];
            string yj = Request.Form["yj"];
            string bz = Request.Form["bz"];
            int id = int.Parse(Request.Form["id"]);
            
            return Json(userCardOper.updateUserCardState(id, card, fy, yj, bz, ((DomainModel.cmsuser)Session["cmsuser"]).id));
        }

        public JsonResult getUserCard()
        {
            int pageno = Convert.ToInt32(Request.Params["pageno"]);
            int pagesize = Convert.ToInt32(Request.Params["pagesize"]);
            string card = Request.Form["card"];
            string in_s = Request.Form["in_s"];
            string in_e = Request.Form["in_e"];
            //return Json(userCardOper.getUserCard(card, in_s, in_e, pageno, pagesize));
            int type = int.Parse(Request.Form["type"]);
            int state = int.Parse(Request.Form["state"]);
            string sx_s = Request.Form["sx_s"];
            string sx_e = Request.Form["sx_e"];
            string dq_s = Request.Form["dq_s"];
            string dq_e = Request.Form["dq_e"];
            int ordercard = int.Parse(Request.Form["ordercard"]);
            int ordertype = int.Parse(Request.Form["ordertype"]);
            int orderstate = int.Parse(Request.Form["orderstate"]);
            int orderdate = int.Parse(Request.Form["orderdate"]);
            int orderuser = int.Parse(Request.Form["orderuser"]);
            return Json(userCardOper.getUserCard(
                card, type, state, in_s, in_e,
                 sx_s, sx_e,
                 dq_s, dq_e,
                 ordercard, ordertype, orderstate, orderdate, orderuser,
                 pageno, pagesize
                ));
        }
        #endregion


        #region 会员收支明细
        public ActionResult IncomeExpenses()
        {
            cmsOper.CheckUserRights("IncomeExpenses");
            return View();
        }
        //
        [ValidateInput(false)]
        public ActionResult saveIncomeExpenses()
        {
            IncomeExpenses _staff = new JavaScriptSerializer().Deserialize<IncomeExpenses>(Request.Form["IncomeExpenses"].ToString());
            if (_staff.ie_id == 0)
            {
                _staff.ie_addTime = DateTime.Now;
                _staff.ie_user = ((DomainModel.cmsuser)Session["cmsuser"]).id;
            }
            else
            {
                _staff.ie_addTime = _staff.ie_addTime.ToLocalTime();
            }
            incomeExpensesOper.save(_staff);
            return Json(_staff);
        }

        public JsonResult getIncomeExpensesbyid()
        {
            string id = Request.Form["res"];
            IList list = incomeExpensesOper.getIncomeExpensesbyid(int.Parse(id));
            if (list.Count > 0)
                return Json(list[0]);
            else
                return Json("");
        }

        public JsonResult deleteIncomeExpenses()
        {
            int id = int.Parse(Request.Form["res"]);

            return Json(incomeExpensesOper.delete(id));
        }

        public JsonResult getIncomeExpenses()
        {
            int pageno = Convert.ToInt32(Request.Params["pageno"]);
            int pagesize = Convert.ToInt32(Request.Params["pagesize"]);
            string in_s = Request.Form["in_s"];
            string in_e = Request.Form["in_e"];
            string name = Request.Form["name"];
            string nick = Request.Form["nick"];
            return Json(incomeExpensesOper.getIncomeExpenses(in_s, in_e, pageno, pagesize,name,nick));
        }
        //
        public JsonResult getUserInfolist()
        {
            string name = Request.Form["name"];
            string nick = Request.Form["nick"];
            return Json(userOper.getUserInfolist(nick,name,""));
        }
       
        #endregion

        public string getUserName()
        {
            int uid = Request.Form["uid"] == null ? 0 : int.Parse(Request.Form["uid"]);
           IList list= userOper.getuserinfobyid(uid);
           if (list.Count > 0)
               return ((Hashtable)list[0])["u_name"].ToString();
           else
               return "";
        }

        public string GenerateCard()
        {
            int cards =int.Parse(Request.Form["cards"].ToString());
            int carde =int.Parse(Request.Form["carde"].ToString());
            string str = "";
            for (int i = cards; i <= carde; i++)
            {
                str += checkMedian(i.ToString()) + "，";
            }
            str = str.Substring(0, str.Length - 1);
            return str;
        }


        public string checkMedian(string oldstr)
        {
            string s="";
            if (oldstr.ToString().Length == 1)
                s = "0000000" + oldstr;
            if (oldstr.ToString().Length == 2)
                s = "000000" + oldstr;
            if (oldstr.ToString().Length == 3)
                s = "00000" + oldstr;
            if (oldstr.ToString().Length == 4)
                s = "0000" + oldstr;
            if (oldstr.ToString().Length == 5)
                s = "000" + oldstr;
            if (oldstr.ToString().Length == 6)
                s = "00" + oldstr;
            if (oldstr.ToString().Length == 7)
                s = "0" + oldstr;
            if (oldstr.ToString().Length == 8)
                s = oldstr;
            return s;
        }
    }
}
